package com.example.wanbaqian.customView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.example.wanbaqian.R;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.Hashtable;

/**
 * Created by Administrator on 2017/4/7.
 */

public class FindCodeView extends AutoRelativeLayout {

    private int BackgroundColor = 0xfffea43f;
    private Paint paint;
    private Bitmap pic;

    public FindCodeView(Context context) {
        this(context, null);
    }

    public FindCodeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FindCodeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setWillNotDraw(false);//必须
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setDither(true);
        paint.setStyle(Paint.Style.FILL);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (Build.VERSION.SDK_INT < 18) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        paint.setColor(BackgroundColor);
        RectF oval = new RectF(0, 0, getWidth(), getHeight());
        canvas.drawRoundRect(oval, dip2px(getContext(), 10), dip2px(getContext(), 10), paint);
        canvas.drawRect(0, 0, getWidth(), getHeight() * 0.5f, paint);
        paint.setColor(Color.WHITE);
        canvas.drawCircle(getWidth() * 0.5f, -getWidth() * 0.15f, getWidth() * 0.28f, paint);

        if (pic != null) {
            float picwidth = getHeight() * 0.55f;
            RectF rectF = new RectF(getWidth() * 0.5f - picwidth * 0.5f, getHeight() * 0.23f, getWidth() * 0.5f + picwidth * 0.5f, getHeight() * 0.23f + picwidth);
            canvas.drawBitmap(pic, null, rectF, null);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public FindCodeView setColor(int Color) {
        BackgroundColor = Color;
        return this;
    }

    public FindCodeView setPic(Bitmap b) {
        pic = b;
        return this;
    }

    public void Build() {
        invalidate();
    }

    public void createQRImage(String url) {
        try {
            String contentString = url;
            if (!contentString.equals("")) {
                //根据字符串生成二维码图片并显示在界面上，第二个参数为图片的大小（600*600）
                Bitmap qrCodeBitmap = createQRCode(contentString, 800);

                //------------------添加logo部分------------------//
                Bitmap logoBmp = BitmapFactory.decodeResource(getResources(), R.drawable.aaa_touxiang);

                //二维码和logo合并
                pic = Bitmap.createBitmap(qrCodeBitmap.getWidth(), qrCodeBitmap
                        .getHeight(), qrCodeBitmap.getConfig());
                Canvas canvas = new Canvas(pic);
                //二维码
                canvas.drawBitmap(qrCodeBitmap, 0, 0, null);
                //logo绘制在二维码中央
                canvas.drawBitmap(logoBmp, qrCodeBitmap.getWidth() / 2
                        - logoBmp.getWidth() / 2, qrCodeBitmap.getHeight()
                        / 2 - logoBmp.getHeight() / 2, null);
                //------------------添加logo部分------------------//
                Build();
            } else {
                //error
            }

        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    public static Bitmap createQRCode(String str, int widthAndHeight) throws WriterException {

        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 1);
        //图像数据转换，使用了矩阵转换
        BitMatrix bitMatrix = new QRCodeWriter().encode(str, BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, hints);
        int[] pixels = new int[widthAndHeight * widthAndHeight];
        //下面这里按照二维码的算法，逐个生成二维码的图片，
        //两个for循环是图片横列扫描的结果
        for (int y = 0; y < widthAndHeight; y++) {
            for (int x = 0; x < widthAndHeight; x++) {
                if (bitMatrix.get(x, y)) {
                    pixels[y * widthAndHeight + x] = 0xff000000;
                } else {
                    pixels[y * widthAndHeight + x] = 0xffffffff;
                }
            }
        }
        //生成二维码图片的格式，使用ARGB_8888
        Bitmap bitmap = Bitmap.createBitmap(widthAndHeight, widthAndHeight, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, widthAndHeight, 0, 0, widthAndHeight, widthAndHeight);
        return bitmap;
    }
}
